//
//  DriveLicenseCell.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/18.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import UIKit
class DriveLicenseCell:UITableViewCell{
    var driveLicense_1,driveLicense_2,driveLicense_3,driveLicense_4,driveLicense_5,submitBtn:UIButton!
    var remark:UITextView!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCellSelectionStyle.none
        self.layer.borderWidth = 0
        //监听键盘弹出通知
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        //监听键盘隐藏通知
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        
        let dlcView = CommonView()
        self.addSubview(dlcView)
        dlcView.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.top.equalTo(self)
            make.width.equalTo(kScreenW - 15)
            make.height.equalTo(commonViewH)
        }

        let dlc = DriveLicenseController()
        let label1 = UILabel.init(frame: CGRect(x: oLabelLeft, y: commonViewH + oTop, width: oLabelW, height: oLabelH))
        label1.font = oFontSize
        label1.text = "驾驶证信息"
        self.addSubview(label1)
        //证件照片
        driveLicense_1 = UIButton.init(frame: CGRect(x: oImgLeft, y: commonViewH + oTop + oLabelH, width: oImgW, height: oImgH))
        driveLicense_1.setBackgroundImage(UIImage(named:"jiashizheng1"), for: .normal)
        driveLicense_1.tag = 1
        driveLicense_1.addTarget(dlc, action: #selector(dlc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        self.addSubview(driveLicense_1)
        
        driveLicense_2 = UIButton.init(frame: CGRect(x: oImgLeft, y: commonViewH + oImgH + oLabelH + 2*oTop, width: oImgW, height: oImgH))
        driveLicense_2.setBackgroundImage(UIImage(named:"jiashizheng2"), for: .normal)
        driveLicense_2.tag = 2
        driveLicense_2.addTarget(dlc, action: #selector(dlc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        self.addSubview(driveLicense_2)
        
        let label2 = UILabel.init(frame: CGRect(x: oLabelLeft, y: commonViewH + 3*oTop + oLabelH + 2*oImgH, width: oLabelW, height: oLabelH))
        label2.font = oFontSize
        label2.text = "身份证信息"
        self.addSubview(label2)
        
        driveLicense_3 = UIButton.init(frame: CGRect(x: oImgLeft, y: commonViewH + 3*oTop + 2*oLabelH + 2*oImgH, width: oImgW, height: oImgH))
        driveLicense_3.setBackgroundImage(UIImage(named:"shenfenzheng1"), for: .normal)
        driveLicense_3.tag = 3
        driveLicense_3.addTarget(dlc, action: #selector(dlc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        self.addSubview(driveLicense_3)
        
        driveLicense_4 = UIButton.init(frame: CGRect(x: oImgLeft, y: commonViewH + 4*oTop + 2*oLabelH + 3*oImgH, width: oImgW, height: oImgH))
        driveLicense_4.setBackgroundImage(UIImage(named:"shenfenzheng2"), for: .normal)
        driveLicense_4.tag = 4
        driveLicense_4.addTarget(dlc, action: #selector(dlc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        self.addSubview(driveLicense_4)
        
        let label3 = UILabel.init(frame: CGRect(x: oLabelLeft, y: commonViewH + 5*oTop + 2*oLabelH + 4*oImgH, width: oLabelW, height: oLabelH))
        label3.font = oFontSize
        label3.text = "正规半身照"
        self.addSubview(label3)
        
        driveLicense_5 = UIButton.init(frame: CGRect(x: oImgLeft, y: commonViewH + 5*oTop + 3*oLabelH + 4*oImgH, width: oImgW, height: oImgH))
        driveLicense_5.setBackgroundImage(UIImage(named:"persion_half"), for: .normal)
        driveLicense_5.tag = 5
        driveLicense_5.addTarget(dlc, action: #selector(dlc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        self.addSubview(driveLicense_5)
        
        let remarkLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: commonViewH + 6*oTop + 3*oLabelH + 5*oImgH, width: oLabelW, height: remarkH))
        remarkLabel.font = oFontSize
        remarkLabel.text = "备注信息"
        self.addSubview(remarkLabel)
        
        remark = UITextView(frame:CGRect(x:oLabelW, y:commonViewH + 6*oTop + 3*oLabelH + 5*oImgH, width:oImgW, height:remarkH))
        remark.layer.cornerRadius = 5
        remark.backgroundColor = tableBgColor
        remark.textColor = fontColor
        remark.font = oFontSize
        remark.delegate = self
        self.addSubview(remark)
        
        //提交
        submitBtn = UIButton.init(frame: CGRect(x: 20, y: commonViewH + 8*oTop + 3*oLabelH + 5*oImgH + remarkH, width: kScreenW - 40, height: cellH))
        submitBtn.setTitleColor(UIColor.white, for: .normal)
        submitBtn.setTitle("提交", for: .normal)
        submitBtn.backgroundColor = appMainColor
        submitBtn.showsTouchWhenHighlighted = true
        submitBtn.layer.cornerRadius = 10
        submitBtn.addTarget(dlc, action: #selector(dlc.submitDriveBtn), for: UIControlEvents.touchUpInside)
        self.addSubview(submitBtn)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension DriveLicenseCell{
    //键盘显示调用
    func keyboardWillShow(_ notification:Notification) {
        if currentTagY > 0 {
            let userInfo = (notification as Notification).userInfo!
            let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
            let statusBarOrientation = UIApplication.shared.statusBarOrientation
            var keyboardHeight:CGFloat = 0.0
            if UIInterfaceOrientationIsPortrait(statusBarOrientation) {
                keyboardHeight = keyboardSize.height
            }else{
                keyboardHeight = keyboardSize.width
            }
            let difference = kScreenH - kStatusBarH - kNavigationBarH - currentTagY - keyboardHeight
            if difference < 0 {
                if difference < -keyboardHeight {
                    self.frame.origin.y = -keyboardHeight
                }else{
                    self.frame.origin.y = difference
                }
            }
        }
    }
    //键盘隐藏调用
    func keyboardWillHide(_ notification:Notification) {
        currentTagY = 0
        self.frame.origin.y = currentTagY
    }
}
extension DriveLicenseCell:UITextViewDelegate{
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        currentTagY = textView.frame.origin.y
        return true
    }
}


